package mes.kanban.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.service.DailyChemicalFirstServiceI;
import mes.kanban.service.PollingConfigServiceI;
import mes.kanban.util.DateUtil;
import mes.kanban.util.IsNotNullValit;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.EchartsDataVO;
import mes.kanban.vo.MesPollingConfigVO;
import mes.kanban.vo.MesWorkshopVO;


@Controller
public class DailyChemicalFirstController {
	
	@Autowired
	private DailyChemicalFirstServiceI dailyChemicalServiceI;
	
	@Autowired
	private PollingConfigServiceI pollingConfigService;
	
	/**
	 * 跳转到查询列表页面，并且加载数据
	 * @param model
	 * @return
	 */
	@GetMapping(value="/main10")
    public String list(Model model, HttpServletRequest request) {
		
		Map<String, Object> map = new HashMap<String,Object>();
		String url = request.getRequestURI().split("/")[1];
		
		//通过URL查询看板配置
		map = pollingConfigService.getConfigByUrl(url);
		
		model.addAttribute("url",url);
        model.addAttribute("distanceTimes",(Long)map.get("distanceTimes"));
        
        if(map.get("tableTimer")!=null) {
			model.addAttribute("timer", (Long)map.get("tableTimer") );
		}
        //nginx映射后的地址
        if (IsNotNullValit.StringIsNotNull((String)map.get("materialName"))) {
        	model.addAttribute("materialName", map.get("materialName"));
		}
        
        // 获得当前的年月日
		String date = DateUtil.DateToString(new Date(),"yyyy年MM月dd日");
        model.addAttribute("date",date);
        
        //通过url查询看板配置
        MesPollingConfigVO configVo = pollingConfigService.findByKanbanUrl(url);
        model.addAttribute("pollingConfigVo",configVo);
        
        return "dailyChemicalFirst/"+ map.get("showType");
        
    }
	
	@GetMapping(value="/main10/queryList")
	@ResponseBody
    public AjaxReturnVO queryList() {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			List<MesWorkshopVO> configs = dailyChemicalServiceI.getDatas();
			vo.setData(configs);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
		
    }
	
	/**
	 * 跳转到查询列表页面，查询产量进度
	 * @param model
	 * @return
	 */
	@GetMapping(value="/main10/queryOutputData")
	@ResponseBody
    public AjaxReturnVO queryOutputData() {
		
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			EchartsDataVO configs = dailyChemicalServiceI.queryOutputData();
			vo.setData(configs);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
		
    }
	
	/**
	 * 跳转到查询列表页面，查询时间进度
	 * @param model
	 * @return
	 */
	@GetMapping(value="/main10/queryTimeScheduleData")
	@ResponseBody
    public AjaxReturnVO queryTimeScheduleData() {
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			EchartsDataVO configs = dailyChemicalServiceI.queryTimeScheduleData();
			vo.setData(configs);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
        
    }
	
	/**
	 * 跳转到查询列表页面，查询当周进度
	 * @param model
	 * @return
	 */
	@GetMapping(value="/main10/queryWeekScheduleData")
	@ResponseBody
    public AjaxReturnVO queryWeekScheduleData() {
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			EchartsDataVO configs = dailyChemicalServiceI.queryWeekScheduleData();
			vo.setData(configs);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
        
    }
	

	
	
	
}
